package com.petsdar.controller;

import com.petsdar.pojo.Emp;
import com.petsdar.service.EmpService;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/5
 * Time: 15:11
 * Description: emp相关操作的controller层
 * Version: V1.0
 */

@RestController
public class EmpController {

    /** emp的业务逻辑处理实例对象 */
    private EmpService empService;

    @Autowired
    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    /**
     * 获取所有员工信息
     * @return
     */
    @GetMapping("/emp")
    public String getEmp(){
        List<Emp> emp = empService.getEmp();
        if (!emp.isEmpty()){
            return ResultUtil.success(emp);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加员工信息
     * @param empJson 员工信息json数据
     * @return
     */
    @PostMapping("/emp")
    public String addEmp(@RequestBody String empJson){
        boolean result = empService.addEmp(empJson);
        if(result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 更新员工信息
     * @param empJson 员工信息json数据
     * @return
     */
    @PutMapping("/emp")
    public String updateEmp(@RequestBody String empJson){
        boolean result = empService.updateEmp(empJson);
        if(result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 删除员工信息
     * @param empId 员工Id
     * @return
     */
    @DeleteMapping("/emp")
    public String delEmp(@RequestParam("empId") String empId){
        boolean result = empService.delEmp(empId);
        if(result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }
}
